using System.ComponentModel.Design;
using PrismFactory.SoftwareFactory.Views.Properties;
using PrismFactory.SoftwareFactory.Views.Validators;

namespace PrismFactory.SoftwareFactory.Views.CreateSolution
{
    public class CollectRootNamespacePageViewModel : ViewModelBase
    {
        public const string RootNamespaceKey = "RootNamespace";
        public const string ShellProjectNameKey = "ShellProjectName";
        public const string InfrastructureProjectNameKey = "InfrastructureProjectName";
        public const string ShellTestProjectNameKey = "ShellTestProjectName";
        public const string InfrastructureTestProjectNameKey = "InfrastructureTestProjectName";

        private readonly IDictionaryService dictionaryService;

        public CollectRootNamespacePageViewModel(IDictionaryService dictionaryService)
        {
            this.dictionaryService = dictionaryService;
            ValidatedProperties = new[]
                                      {
                                          RootNamespaceKey,
                                      };
            UpdateProjectNames();
        }

        public string RootNamespace
        {
            get { return (string) dictionaryService.GetValue(RootNamespaceKey); }
            set
            {
                dictionaryService.SetValue(RootNamespaceKey, value);
                UpdateProjectNames();
                OnPropertyChanged(RootNamespaceKey);
            }
        }

        public string ShellProjectName
        {
            get { return (string) dictionaryService.GetValue(ShellProjectNameKey); }
            private set
            {
                dictionaryService.SetValue(ShellProjectNameKey, value);
                OnPropertyChanged(ShellProjectNameKey);
            }
        }

        public string InfrastructureProjectName
        {
            get { return (string) dictionaryService.GetValue(InfrastructureProjectNameKey); }
            private set
            {
                dictionaryService.SetValue(InfrastructureProjectNameKey, value);
                OnPropertyChanged(InfrastructureProjectNameKey);
            }
        }

        public string ShellTestProjectName
        {
            get { return (string) dictionaryService.GetValue(ShellTestProjectNameKey); }
            private set
            {
                dictionaryService.SetValue(ShellTestProjectNameKey, value);
                OnPropertyChanged(ShellTestProjectNameKey);
            }
        }

        public string InfrastructureTestProjectName
        {
            get { return (string) dictionaryService.GetValue(InfrastructureTestProjectNameKey); }
            private set
            {
                dictionaryService.SetValue(InfrastructureTestProjectNameKey, value);
                OnPropertyChanged(InfrastructureTestProjectNameKey);
            }
        }

        private void UpdateProjectNames()
        {
            ShellProjectName = BuildShellProjectName();
            InfrastructureProjectName = BuildInfrastructureProjectName();
            ShellTestProjectName = BuildShellTestProjectName();
            InfrastructureTestProjectName = BuildInfrastructureTestProjectName();
        }

        private string BuildInfrastructureProjectName()
        {
            return RootNamespace + ".Infrastructure";
        }

        private string BuildShellProjectName()
        {
            return RootNamespace + ".Shell";
        }

        private string BuildInfrastructureTestProjectName()
        {
            return BuildInfrastructureProjectName() + ".Tests";
        }

        private string BuildShellTestProjectName()
        {
            return BuildShellProjectName() + ".Tests";
        }

        protected override void DoPropertyValidation(string propertyName, ref string error)
        {
            switch (propertyName)
            {
                case RootNamespaceKey:
                    var namespaceValidator = new NamespaceValidator();
                    if (!namespaceValidator.Validate(RootNamespace).IsValid)
                        error = Resources.InvalidNamespace;
                    break;
            }
        }
    }
}